package com.ke.jun.view.widght;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class TransparentCircle extends View {
    private String TAG="TransparentCircle";
    Bitmap bm;

    Canvas cv;

    Paint eraser;

    public TransparentCircle(Context context) {
        super(context);

        Init();
        Log.e(TAG,"MyView()");

    }

    public TransparentCircle(Context context, AttributeSet attrs) {
        super(context, attrs);

        Init();
        Log.e(TAG,"MyView()");

    }

    public TransparentCircle(Context context, AttributeSet attrs,

                             int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        Init();
        Log.e(TAG,"MyView()");
    }

    private void Init() {
        eraser = new Paint();

        eraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

        eraser.setAntiAlias(true);

    }

    @Override

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (w != oldw || h != oldh) {
            bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

            cv = new Canvas(bm);

        }

        super.onSizeChanged(w, h, oldw, oldh);

        Log.e(TAG,"onSizeChanged()");

    }

    @Override

    protected void onDraw(Canvas canvas) {
        Log.e(TAG,"onDraw()");
        int w = getWidth();

        int h = getHeight();

        int radius = w > h ? h / 2 : w / 2;

        bm.eraseColor(Color.TRANSPARENT);

        cv.drawColor(Color.BLUE);

        cv.drawCircle(w / 2, h / 2, radius, eraser);

        canvas.drawBitmap(bm, 0, 0, null);

        super.onDraw(canvas);

    }

}